package com.gec.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.entity.SysOwner;
import com.gec.model.vo.SysOwnerQueryVo;
import com.gec.system.service.SysOwnerService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 业主表 前端控制器
 * </p>
 *
 * @author lys
 * @since 2023-12-06
 */

@CrossOrigin
@Api(tags = "业主管理控制器")
@RestController
@RequestMapping("/admin/system/sysOwner/")
public class SysOwnerController {

    @Autowired
    private SysOwnerService sysOwnerService;

//    @Autowired
//    private SysOwner sysOwner;


    @ApiOperation("分页查询业主列表")
    @GetMapping("{page}/{limit}")
    public Result findPage(@PathVariable Long page,
                           @PathVariable Long limit,
                           SysOwnerQueryVo vo){

        IPage<SysOwner> page1 = new Page<>(page,limit);
        page1 = this.sysOwnerService.selectPageList(page1,vo);
        return Result.ok(page1);
    }

    @ApiOperation("添加业主信息")
    @PostMapping("addOwner")
    public Result addOwner(@RequestBody SysOwner sysOwner){

        boolean save = this.sysOwnerService.save(sysOwner);
        if (save){
            return Result.ok();
        }

        return Result.fail();
    }

    @ApiOperation("根据id查询业主信息")
    @GetMapping("getOwnerById/{id}")
    public Result getOwnerById(@PathVariable Long id){
        QueryWrapper<SysOwner> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        SysOwner one = this.sysOwnerService.getOne(wrapper);
        return Result.ok(one);
    }

    @ApiOperation("根据tel查询业主信息")
    @GetMapping("getOwnerByTel/{telephone}")
    public Result getOwnerByTel(@PathVariable String telephone){

        SysOwner one = this.sysOwnerService.getOwnerByTel(telephone);
        return Result.ok(one);
    }

    @ApiOperation("根据id修改业主信息")
    @PostMapping("updateOwnerById")
    public Result updateOwnerById(@RequestBody SysOwner sysOwner){

        boolean b = this.sysOwnerService.updateById(sysOwner);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("用 id  解绑业主信息")
    @PostMapping("closeBindOwnerById/{id}")
    public Result closeBindOwnerById(@PathVariable Long id){
        SysOwner sysOwner=new SysOwner();
        sysOwner.setIsBind(1);
        UpdateWrapper<SysOwner> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        boolean b = this.sysOwnerService.update(sysOwner,wrapper);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

//    根据手机号码/小区号/房号查询，是否存在该业主信息
//    有=》继续，将用户的数据添加
//    没=》返回提示信息
    @ApiOperation("绑定业主信息")
    @PostMapping("bindOwner")
    public Result bindOwner(@RequestBody SysOwner sysOwner){
//        System.out.println("sysOwner:"+sysOwner.toString());
        boolean b =this.sysOwnerService.bindOwner(sysOwner);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }



    @ApiOperation("根据id删除业主信息")
    @DeleteMapping("removeOwnerById/{id}")
    public Result removeOwnerById(@PathVariable Long id){

        boolean b = this.sysOwnerService.removeById(id);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("根据ids批量删除业主信息")
    @DeleteMapping("removeOwnerByIds")
    public Result removeOwnerByIds(@RequestBody List<Long> ids){

        boolean b = this.sysOwnerService.removeByIds(ids);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

}

